package reference;

import reference.vo.ComponentVo;
import reference.vo.UieComponentVo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author hujian
 * @date 2023/2/17 21:37
 * @description
 */
public class RefUtils {

    //取出字符串中的中文子串
    public static List<ComponentVo> getComponentVo(String str) {

        Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]+");
        Matcher matcher = pattern.matcher(str);

        int start = 0;
        List<ComponentVo> componentVoList = new ArrayList<>();
        while (matcher.find()) {
            ComponentVo componentVo = new ComponentVo();
            if (matcher.start() > start) {
                componentVo.setBeforeSymbol(str.substring(start, matcher.start()));
            }
            componentVo.setText(matcher.group());
            componentVoList.add(componentVo);
            start = matcher.end();
        }
        if (start < str.length() && start > 0) {
            componentVoList.get(componentVoList.size() - 1).setAfterSymbol(str.substring(start));
        }
        if (componentVoList.size() > 1) {
            for (int i = 0; i < componentVoList.size() - 1; i++) {
                componentVoList.get(i).setAfterSymbol(componentVoList.get(i + 1).getBeforeSymbol());
            }
        }
        return componentVoList;
    }

    //取到中文开头中文结尾的做标题
    public static String getTitle(String str) {
        Pattern pattern = Pattern.compile("[\u4e00-\u9fa5]+.*[\u4e00-\u9fa5]+");
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            return matcher.group();
        } else {
            return null;
        }
    }

    //取到英文开头英文结尾
    public static String getEnSeq(String str) {
        Pattern pattern = Pattern.compile("[\\dA-Za-z]+.*[A-Za-z]+");
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            return matcher.group();
        } else {
            return null;
        }
    }

    public static String getEnAuthorSeq(String str) {
        Pattern pattern = Pattern.compile("[A-Za-z]+.*[A-Za-z]+");
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            return matcher.group();
        } else {
            return null;
        }
    }

    //提取汉字
    public static String getCnTxt(String string) {
        String reg = "[^\u4e00-\u9fa5]";
        string = string.replaceAll(reg, "");
        return string;
    }

    //提取字母
    public static String getEnTxt(String string) {
        String reg = "[^A-Z]";
        string = string.replaceAll(reg, "");
        return string;
    }

    //满足某个正则的数量
    public static int countPattern(String str, String patternStr) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(str);
        int count = 0;
        while (matcher.find()) {
            count++;
        }
        return count;
    }

    public static String findPattern(String str, String patternStr) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(str);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

    public static Boolean matchPattern(String str, String patternStr) {
        Pattern pattern = Pattern.compile(patternStr);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    //全角转半角
    public static String toDBC(String input) {
        if (input == null) {
            return null;
        }
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {  // 全角空格为12288
                c[i] = (char) 32;  // 将全角空格转换为半角空格
            } else if (c[i] >= 65281 && c[i] <= 65374) {  // 全角字符（除空格）的Unicode范围为65281~65374
                c[i] = (char) (c[i] - 65248);  // 转换为半角字符
            }
        }
        return new String(c);
    }

    //判断是否有全角
    public static boolean hasDBC(String input) {
        if (input == null) {
            return false;
        }
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c >= 65281 && c <= 65374) { // 全角字符的Unicode范围为65281~65374
                return true;
            }
        }
        return false;
    }

    //获取参考文献中的书名信息
    public static List<UieComponentVo> getBooks(String input) {
        Pattern pattern = Pattern.compile("《[^》]+》"); // 匹配书名号内的内容
        Matcher matcher = pattern.matcher(input);
        List<UieComponentVo> uieBookVos = new ArrayList<>();
        while (matcher.find()) {
            UieComponentVo uieComponentVo = new UieComponentVo();
            uieComponentVo.setStart(matcher.start());
            uieComponentVo.setEnd(matcher.end());
            uieComponentVo.setText(matcher.group());
            uieBookVos.add(uieComponentVo);
        }
        return uieBookVos;
    }

    //得到以中文开始中文结束的最长序列
    public static String getCnSeq(String input) {
        Pattern pattern = Pattern.compile("[\\u4e00-\\u9fa5].*[\\u4e00-\\u9fa5]"); // 匹配汉字开始、汉字结束的字符串
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

    //查找小数点的位置
    public static List<Integer> findDecimalPoints(String s) {
        List<Integer> indices = new ArrayList<Integer>();
        Pattern pattern = Pattern.compile("\\.");
        Matcher matcher = pattern.matcher(s);
        while (matcher.find()) {
            indices.add(matcher.start());
        }
        return indices;
    }

    //并重排,大致将参考文献分成三份

    public static List<Integer> divideArray(int[] arr) {
        System.out.println(Arrays.toString(arr));
        int n = arr.length;
        int[] distances = new int[n - 1];
        for (int i = 0; i < n - 1; i++) {
            distances[i] = arr[i + 1] - arr[i];
        }
        int max1 = 0;
        int max2 = 0;
        for (int i = 1; i < n - 1; i++) {
            if (distances[i] > distances[max1]) {
                max2 = max1;
                max1 = i;
            } else if (distances[i] > distances[max2]) {
                max2 = i;
            }
        }
        if (max1 > max2) {
            int tmp = max1;
            max1 = max2;
            max2 = tmp;
        }
        int[][] result = new int[3][];
        result[0] = Arrays.copyOfRange(arr, 0, max1 + 1);
        result[1] = Arrays.copyOfRange(arr, max1 + 1, max2 + 1);
        result[2] = Arrays.copyOfRange(arr, max2 + 1, n);

        List<Integer> threeIndices = new ArrayList<>();
        if (result[0].length > 0) {
            threeIndices.add(result[0][result[0].length - 1]);
        }
        if (result[1].length > 0) {
            threeIndices.add(result[1][result[1].length - 1]);
        }
        if (result[2].length > 0) {
            threeIndices.add(result[2][result[2].length - 1]);
        }
        return threeIndices;
    }

    public static void main(String[] args) {

        System.out.println(getComponentVo("吴天妹，卢黎莉"));
    }
}
